from django.db import models
from user_app.models import User


class Vehicle(models.Model):
    """车辆信息模型"""
    
    # 车辆状态选项
    class Status(models.TextChoices):
        AVAILABLE = 'available', '空闲'
        IN_TRANSIT = 'in_transit', '运输中'
        MAINTENANCE = 'maintenance', '维护中'
        UNAVAILABLE = 'unavailable', '不可用'
    
    # 车辆类型选项
    class Type(models.TextChoices):
        TANK_TRUCK = 'tank_truck', '罐车'
        FLAT_BED = 'flat_bed', '平板车'
        BOX_TRUCK = 'box_truck', '厢式货车'
        OTHER = 'other', '其他'
    
    # 基本信息
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='vehicles', verbose_name='车主')
    license_plate = models.CharField(max_length=20, unique=True, verbose_name='车牌号')
    vehicle_type = models.CharField(max_length=20, choices=Type.choices, default=Type.TANK_TRUCK, verbose_name='车辆类型')
    
    # 车辆规格
    capacity = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='载重量(吨)')
    length = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True, verbose_name='车长(米)')
    width = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True, verbose_name='车宽(米)')
    height = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True, verbose_name='车高(米)')
    
    # 位置信息
    current_location = models.CharField(max_length=255, null=True, blank=True, verbose_name='当前位置')
    longitude = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True, verbose_name='经度')
    latitude = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True, verbose_name='纬度')
    location_updated_at = models.DateTimeField(null=True, blank=True, verbose_name='位置更新时间')
    
    # 状态信息
    status = models.CharField(max_length=20, choices=Status.choices, default=Status.AVAILABLE, verbose_name='状态')
    available_time = models.DateTimeField(null=True, blank=True, verbose_name='可用时间')
    
    # 车辆证件
    vehicle_license = models.CharField(max_length=50, null=True, blank=True, verbose_name='行驶证号')
    vehicle_license_image = models.URLField(max_length=255, null=True, blank=True, verbose_name='行驶证照片')
    vehicle_image = models.URLField(max_length=255, null=True, blank=True, verbose_name='车辆图片')
    
    # 审核状态
    is_approved = models.BooleanField(default=False, verbose_name='是否已审核通过')
    
    # 时间戳
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        verbose_name = '车辆信息'
        verbose_name_plural = '车辆信息'
        ordering = ['-created_at']
    
    def __str__(self):
        return f"{self.license_plate} - {self.get_vehicle_type_display()} ({self.user.phone})"